package com.xunshibao.core.spring;

import java.util.Date;
import java.util.HashMap;

import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorException;
import org.apache.commons.validator.ValidatorResults;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.Errors;
import org.springframework.validation.MapBindingResult;

import com.xunshibao.spring.validator.SpringExtValidatorFactory;

public class StartupCtx {
	@Test
	public void start(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("test/spring.xml");
		SpringExtValidatorFactory factory = ctx.getBean(SpringExtValidatorFactory.class);
		
		User user = new User();
		user.setBirthday(new Date());
		Errors errors = new MapBindingResult(new HashMap(), "test");
		Validator validator = factory.getValidator("registrationForm", user, errors);
		
		try {
			 ValidatorResults results = validator.validate();
			 System.out.println(results.isEmpty());
		} catch (ValidatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static class User {
		private String firstName;
		private String lastName;
		private Date birthday;
		
		public Date getBirthday() {
			return birthday;
		}
		public void setBirthday(Date birthday) {
			this.birthday = birthday;
		}
		public String getFirstName() {
			return firstName;
		}
		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}
		public String getLastName() {
			return lastName;
		}
		public void setLastName(String lastName) {
			this.lastName = lastName;
		}
	}
}

